package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.GestureDetector.OnGestureListener;

import com.timothy.android.BaseAnswerActivity;
import com.timothy.android.answer.Bar2DAnswerActivity.HorizontalBar2DView;
import com.timothy.android.answer.Bar2DAnswerActivity.VerticalBar2DView;
import com.timothy.android.util.NumberUtil;
import com.timothy.android.util.ValueCopyUtil;
import com.timothy.chart.Bar3D;
import com.timothy.chart.BaseChart;
import com.timothy.chart.CanvasSetting;

public class Bar3DAnswerActivity extends BaseAnswerActivity{
	private final static String LOG_TAG = "Bar3DAnswerActivity";
	List<Bar3D> barList = new ArrayList<Bar3D>();
	SurfaceView view = null;
	
	public void setView(int orientation){
		switch(orientation){
			case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:view = new VerticalBar3DView(this);break;
			case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:view = new HorizontalBar3DView(this);break;
			default:view = new VerticalBar3DView(this);
		}
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG,"onCreate");
//		this.setTitle("Bar3DAnswer");
		this.appendTitle("-Bar3DAnswer");
		
		Bundle bundle = this.getIntent().getExtras();
		String displayStyle = bundle.getString("displayStyle");
		
		for(int i=0;i<chartList.size();i++){
			BaseChart baseChart = chartList.get(i);
			Bar3D bar3D = new Bar3D(8);
			try {
				ValueCopyUtil.valueCopy(baseChart, bar3D);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			barList.add(bar3D);
		}
		
		
		if("3".equals(displayStyle)){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			setView(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);			
		}else if("4".equals(displayStyle)){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			setView(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);			
		}
		
		setContentView(view);
	}

	
	class VerticalBar3DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;
		private final static float BAR_RATIO = 0.8f;
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public VerticalBar3DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalBar3DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalBar3DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalBar2DView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalBar2DView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalBar2DView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalBar2DView:bottomM:"+bottomM);
			
			//set background color
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
//			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY,mPaint); 
			
			
			String questionString = baseBean.getQuestionText();
			if("42".equals(type)&&priority!=null){
				questionString = questionString + "(priority = "+priority+")";
			}
			
			int queLength = questionString.length();
			Log.i(LOG_TAG, "HorizontalBar2DView:queLength:"+queLength);
			
			int line = 0;
			int lineHeight = 0;
			if(queLength>QUESTION_WRAP_LENGTH_VERTIAL){
				Log.i(LOG_TAG, "VerticalBar3DView:draw question text->queLength>"+QUESTION_WRAP_LENGTH_VERTIAL);
				for(line = 0;line<queLength/QUESTION_WRAP_LENGTH_VERTIAL;line++){
					canvas.drawText(questionString.substring(QUESTION_WRAP_LENGTH_VERTIAL*line,QUESTION_WRAP_LENGTH_VERTIAL*(line+1)),queX,queY/4+lineHeight,mPaint);
					Log.i(LOG_TAG, "VerticalBar3DView:start:"+QUESTION_WRAP_LENGTH_VERTIAL*line+";end:"+QUESTION_WRAP_LENGTH_VERTIAL*(line+1));
					lineHeight+= queY/4;
				}
				Log.i(LOG_TAG, "VerticalBar3DView:start:"+QUESTION_WRAP_LENGTH_VERTIAL*line);
				canvas.drawText(questionString.substring(line*QUESTION_WRAP_LENGTH_VERTIAL),queX,queY/4+lineHeight,mPaint);
			}else{
				Log.i(LOG_TAG, "VerticalBar3DView:draw question text->queLength<="+QUESTION_WRAP_LENGTH_VERTIAL);
				canvas.drawText(questionString,queX,queY/2,mPaint);
			}
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "VerticalBar3DView:spaceInCharts:"+setting.getSpace());
			int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
			double barWidthInclSpace = (canvasNoMarginWidth * 1.0)/barList.size();
			int barWidth = (int)(barWidthInclSpace*0.9);
			setting.setSpace((int)(barWidthInclSpace-barWidth));
			Log.i(LOG_TAG, "VerticalBar3DView:barWidth:"+barWidth);
			
			int canvasNoMarginHeight = canvasHeight-setting.getTopMargin()-setting.getBottomMargin();
			
			for(int i=0;i<barList.size();i++){
				Bar3D bar3D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				
				int votes = bar3D.getVotes();
				double votesShare = bar3D.getShare();
				int barHeigth = (int) (votesShare * canvasNoMarginHeight * BAR_RATIO);
				
				Log.i(LOG_TAG, "VerticalBar3DView:barHeigth:"+barHeigth);
				Log.i(LOG_TAG, "VerticalBar3DView:votes:"+votes);
				Log.i(LOG_TAG, "VerticalBar3DView:votesShare:"+votesShare);
				
				int tlX = (int) (setting.getLeftMargin()+ i * (barWidth + setting.getSpace()));
				int tlY =  canvasHeight - setting.getBottomMargin() - barHeigth;
				int brX = tlX + barWidth;
				int brY = canvasHeight - setting.getBottomMargin();
				
				Log.i(LOG_TAG, "VerticalBar3DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				
				int thickness = bar3D.getThickness();
				
				Log.i(LOG_TAG, "thickness:"+thickness);
				
				for(int j=0;j<thickness;j++){
					String color = bar3D.getShadowColor();
					Log.i(LOG_TAG, "Color:"+color);
					mPaint.setColor(Color.parseColor(color));
					canvas.drawRect(tlX++,tlY--,brX++,brY--, mPaint);					
				}
				
				mPaint.setColor(Color.parseColor(bar3D.getFullColor()));
				canvas.drawRect(tlX-thickness,tlY+thickness,brX-thickness,brY+thickness, mPaint);
				canvas.save();
				
				//draw answer text
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX:AnswerTextSize="+bar3D.getAnswerTextSize());
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX:AnswerTextColor="+bar3D.getAnswerTextColor());
				mPaint.setTextSize(bar3D.getAnswerTextSize());
				mPaint.setColor(Color.parseColor(bar3D.getAnswerTextColor()));
				
				String formatShare = "0%";
				if(bar3D.getShare()*100>0){
					formatShare = NumberUtil.formatData1Deci(bar3D.getShare()*100)+"%";
				}
				canvas.drawText(formatShare, tlX, tlY-20, mPaint);
				canvas.save();
				
				//draw choice
				Log.i(LOG_TAG, "VerticalBar3DView:ChoiceTextSize="+bar3D.getChoiceTextSize());
				Log.i(LOG_TAG, "VerticalBar3DView:ChoiceTextColor="+bar3D.getChoiceTextColor());
				mPaint.setTextSize(bar3D.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(bar3D.getChoiceTextColor()));
				canvas.drawText(bar3D.getChoiceText(), tlX, canvasHeight-setting.getBottomMargin()/2, mPaint);
				canvas.save();
			}

			//draw border
			mPaint.setColor(Color.WHITE);
			for(int x=1;x<10;x++){
				//draw horizontal lines
				canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
			}
			
			for(int y=1;y<10;y++){
				//draw vertical lines
				canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
			}
			canvas.save();
			
			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}

	class HorizontalBar3DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public HorizontalBar3DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "HorizontalBar3DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "HorizontalBar3DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,2);
			
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalBar2DView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalBar2DView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalBar2DView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalBar2DView:bottomM:"+bottomM);
			
//			//write question
//			Paint mPaint = new Paint();
//			mPaint.setColor(Color.parseColor(questionTextColor));		
//			mPaint.setTextSize(textSize);
//			int queX = setting.getLeftMargin();
//			int queY = setting.getTopMargin()/2;
//			Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
//			Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
//			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY,mPaint); 
//			canvas.save();
//			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "HorizontalBar2DView:queX:"+queX);
			Log.i(LOG_TAG, "HorizontalBar2DView:queY:"+queY);
			
			String questionString = baseBean.getQuestionText();
			if("42".equals(type)&&priority!=null){
				questionString = questionString + "(priority = "+priority+")";
			}
			
//			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY/2,mPaint); 
			
			int queLength = questionString.length();
			Log.i(LOG_TAG, "HorizontalBar2DView:queLength:"+queLength);
			
			int line = 0;
			int lineHeight = 0;
			if(queLength>QUESTION_WRAP_LENGTH_HORIZONTAL){
				Log.i(LOG_TAG, "HorizontalBar2DView:draw question text->queLength>"+QUESTION_WRAP_LENGTH_HORIZONTAL);
				for(line = 0;line<queLength/QUESTION_WRAP_LENGTH_HORIZONTAL;line++){
					canvas.drawText(questionString.substring(QUESTION_WRAP_LENGTH_HORIZONTAL*line,QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1)),queX,queY/2+lineHeight,mPaint);
					Log.i(LOG_TAG, "HorizontalBar2DView:start:"+QUESTION_WRAP_LENGTH_HORIZONTAL*line+";end:"+QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1));
					lineHeight+= queY/2;
				}
				Log.i(LOG_TAG, "HorizontalBar2DView:start:"+QUESTION_WRAP_LENGTH_HORIZONTAL*line);
				canvas.drawText(questionString.substring(line*QUESTION_WRAP_LENGTH_HORIZONTAL),queX,queY/2+lineHeight,mPaint);
			}else{
				Log.i(LOG_TAG, "HorizontalBar2DView:draw question text->queLength<="+QUESTION_WRAP_LENGTH_HORIZONTAL);
				canvas.drawText(questionString,queX,queY/2,mPaint);
			}
			
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "HorizontalBar3DView:spaceInCharts:"+setting.getSpace());
//			int barWidth = (canvasWidth - setting.getLeftMargin() - setting.getRightMargin())/barList.size() - (int) setting.getSpaceInCylis();
			int barHeight = (canvasHeight - setting.getTopMargin() - setting.getBottomMargin())/barList.size() - (int) setting.getSpace();
			Log.i(LOG_TAG, "HorizontalBar3DView:barHeight:"+barHeight);
			
			
			int canvasNoMarginWidth = canvasWidth-setting.getLeftMargin()-setting.getRightMargin();
			
			for(int i=0;i<barList.size();i++){
				Bar3D bar3D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				
				int votes = bar3D.getVotes();
				double votesShare = bar3D.getShare();
//				int barHeigth = (int) (votesShare * canvasHeight);
				int barWidth = (int) (votesShare * canvasNoMarginWidth);
				
				Log.i(LOG_TAG, "HorizontalBar3DView:barWidth:"+barWidth);
				Log.i(LOG_TAG, "HorizontalBar3DView:votes:"+votes);
				Log.i(LOG_TAG, "HorizontalBar3DView:votesShare:"+votesShare);
				
				int tlX = setting.getLeftMargin();
				int tlY = setting.getTopMargin()+ (int)(i*(barHeight + setting.getSpace()));
				int brX = tlX + barWidth;
				int brY = tlY + barHeight;
				
				Log.i(LOG_TAG, "HorizontalBar3DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				
				int thickness = bar3D.getThickness();
				
				for(int j=0;j<thickness;j++){
					mPaint.setColor(Color.parseColor(bar3D.getShadowColor()));
					canvas.drawRect(tlX++,tlY++,brX++,brY++, mPaint);	
				    canvas.save();
				}
				
				mPaint.setColor(Color.parseColor(bar3D.getFullColor()));
				canvas.drawRect(tlX-thickness,tlY-thickness,brX-thickness,brY-thickness, mPaint);
//				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				canvas.save();
				
				//draw answer
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX:AnswerTextSize="+bar3D.getAnswerTextSize());
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX:AnswerTextColor="+bar3D.getAnswerTextColor());
				mPaint.setTextSize(bar3D.getAnswerTextSize());
				mPaint.setColor(Color.parseColor(bar3D.getAnswerTextColor()));
				String formatAnswer = "0 0%";
				if(bar3D.getVotes()>0){
					formatAnswer = bar3D.getVotes() + " " + NumberUtil.formatData1Deci(bar3D.getShare()*100)+"%";
				}
				canvas.drawText(formatAnswer, tlX + barWidth+10, tlY+barHeight/2, mPaint);
				canvas.save();
				
				//draw choice
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceTextSize="+bar3D.getChoiceTextSize());
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceTextColor="+bar3D.getChoiceTextColor());
				mPaint.setTextSize(bar3D.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(bar3D.getChoiceTextColor()));
				canvas.drawText(bar3D.getChoiceText(),10, tlY+barHeight/2, mPaint);
				canvas.save();
			}
			
			//draw border
			mPaint.setColor(Color.WHITE);
			for(int x=1;x<10;x++){
				//draw horizontal lines
				canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
			}
			for(int y=1;y<10;y++){
				//draw vertical lines
				canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
			}
			canvas.save();

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}
}
